﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using HomeBuyerTools;
using System.Xml.Linq;
using LocalSettings;
using Microsoft.Advertising.Mobile.UI;
using HomeBuyerTools.ViewModels;
using HomeBuyerTools.FinanceProperties;

namespace FinanceCalculators.Views.MortagePayment
{
    public partial class MortagePayment : PhoneApplicationPage
    {
        private SolidColorBrush adcontrolborder = new SolidColorBrush(Colors.Transparent);
        Custom objCustom = new Custom();
        public MortagePayment()
        {
            InitializeComponent();
            LoadPivotThemes();
            REg.Value = DateTime.Now;
            if (Appsettings.RegstrationDate != "")
            {
                REg.Value = Convert.ToDateTime(Appsettings.RegstrationDate);
            }

        }
        private void LoadPivotThemes()
        {
            LayoutRoot.Background =  Custom.getPivotBackground();
            
        }
     
        Settings savadata = new Settings();
        //private void textBox5_LostFocus(object sender, RoutedEventArgs e)
        //{
        //    if (txtPrice.Text != "")
        //    {
        //        double DownPayement = Convert.ToInt32(txtPrice.Text) * 0.2;
        //        txtDownPayment.Text = DownPayement.ToString();
        //    }
        //    else
        //    {
        //        MessageBox.Show("Please enter Price");
        //    }
        //}

        //private void textBox3_LostFocus(object sender, RoutedEventArgs e)
        //{
        //    if (txtLoanAmount.Text != "")
        //    {
        //        double terms = Convert.ToDouble(txtTermOfLoan.Text);
        //        double interest = Convert.ToDouble(txtInterestRate.Text);
        //        double loanamount = Convert.ToDouble(txtLoanAmount.Text);
        //        double paymentVal = loanamount * (interest / 100 / 12 / (1 - Math.Pow((1 + interest / 100 / 12), (-terms))));
        //        paymentVal = Math.Round(paymentVal, 2);
        //        tbPayment.Text = paymentVal.ToString();
        //    }
        //    else
        //    {
        //        MessageBox.Show("Please enter Terms,Intrest and LoanAmount");
        //    }

        //}





        //private void ApplicationBarIconButton_Click(object sender, EventArgs e)
        //{
        //    double DownPayement = Convert.ToInt32(txtPrice.Text) * 0.2;
        //    txtDownPayment.Text = DownPayement.ToString();
        //    txtDownPayment.Visibility = Visibility.Visible;
        //    double TermOfLoan = Convert.ToDouble(txtTermOfLoan.Text);
        //    double IntrestRate = Convert.ToDouble(txtInterestRate.Text);
        //    double LoanAmount = Convert.ToDouble(txtLoanAmount.Text);
        //    double paymentVal =Math.Round(LoanAmount * (IntrestRate / 100 / 12 / (1 - Math.Pow((1 + IntrestRate / 100 / 12), (-TermOfLoan)))),2);
        //    tbPayment.Text = Convert.ToString(paymentVal);
        //    tbPayment.Visibility = Visibility.Visible;

        //}
        //private bool errorreport;
        //public bool ErrorReport
        //{
        //    get
        //    {
        //        bool isTextBoxEmpty = false;
        //        if (txtLoanAmount.Text != ""  && txtPrice.Text != "" && txtInterestRate.Text != "" && txtTermOfLoan.Text != "" )
        //            isTextBoxEmpty = true;

        //        return isTextBoxEmpty;
        //    }
        //}
        //MortageSaveData savadata = new MortageSaveData();
        //if (txtLoanAmount.Text != "")
        //{
        //    savadata.AddDetails(Convert.ToInt32(txtTermOfLoan.Text), Convert.ToDouble(txtInterestRate.Text), Convert.ToDouble(txtLoanAmount.Text), Convert.ToDouble(tbPayment.Text), Convert.ToDouble(txtPrice.Text), Convert.ToDouble(txtDownPayment.Text));
        //}
        //else
        //{
        //    MessageBox.Show("Please enter values");
        //}


        private void ApplicationBarIconButton_Click_1(object sender, EventArgs e)
        {
            if (Appsettings.RegstrationDate != REg.Value.ToString())
            {
                Appsettings.DiffMonths = 0;
                DateTime currentdat = DateTime.Now;
                DateTime previousdate =Convert.ToDateTime(REg.Value);
                int diffMonths = 0;
                double totalmonths;
                int currentyear = currentdat.Year;
                if (previousdate.Year < currentdat.Year)
                {
                    double totaldays = (currentdat - previousdate).TotalDays;
                    double m = totaldays / 30;
                    totalmonths = (Math.Floor(m));
                    Appsettings.DiffMonths = Convert.ToInt32(totalmonths);
                }

                else
                {

                    if (currentyear == previousdate.Year)
                    {
                        diffMonths = currentdat.Month - previousdate.Month;
                        if (currentdat.Day > previousdate.Day)
                        {
                            Appsettings.DiffMonths = diffMonths + 1;
                            string diff = diffMonths.ToString();
                            if (diff.StartsWith("-"))
                            {
                                Appsettings.DiffMonths = 0;
                            }
                        }
                        else
                        {
                            Appsettings.DiffMonths = diffMonths;

                            string diff = diffMonths.ToString();
                            if (diff.StartsWith("-"))
                            {
                                Appsettings.DiffMonths = 0;
                            }
                        }
                    }
                    else
                    {
                        Appsettings.DiffMonths = 0;
                    }
                }
                Appsettings.TotalSumForSalesPrice = 0;
                Appsettings.LoanTream = 0;
                Appsettings.CompareMonth = 0;
            }
            Appsettings.RegstrationDate = REg.Value.ToString();
            NavigationService.Navigate(new Uri("/Views/Result/ResultPage.xaml?sectionId=1&LA=" + txtLoanAmount.Text + "&IR=" + txtInterestRate.Text + "&TL=" + txtTermOfLoan.Text + "&Pri=" + txtPrice.Text + "&HC=" + txtHomeCoast.Text, UriKind.Relative));
        }
        private void txtInterestRate_LostFocus(object sender, RoutedEventArgs e)
        {


            savadata.AddInterest(Convert.ToDouble(txtInterestRate.Text),0);

        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            Visibility darkBackgroundVisibility = (Visibility)Application.Current.Resources["PhoneDarkThemeVisibility"];

            if (darkBackgroundVisibility == Visibility.Visible)
            {
                //Theme is Dark
            }

            else
            {
                txtLoanAmount.MouseEnter += txtLoanAmount_MouseEnter;
                txtLoanAmount.MouseLeave += txtLoanAmount_MouseLeave;
                txtInterestRate.MouseEnter += txtInterestRate_MouseEnter;
                txtInterestRate.MouseLeave += txtInterestRate_MouseLeave;
                txtTermOfLoan.MouseEnter += txtTermOfLoan_MouseEnter;
                txtTermOfLoan.MouseLeave += txtTermOfLoan_MouseLeave;
                txtPrice.MouseEnter += txtPrice_MouseEnter;
                txtPrice.MouseLeave += txtPrice_MouseLeave;
                txtHomeCoast.MouseEnter += txtHomeCoast_MouseEnter;
                txtHomeCoast.MouseLeave += txtHomeCoast_MouseLeave;
                //Theme is Light

            }
            savadata.AddInterest(Convert.ToDouble(txtInterestRate.Text),0);
            //txtInterestRate.Text = savadata.loadInterest();
            List<SettingsProperties> prop = savadata.loadInterest();
            foreach (SettingsProperties irate in prop)
            {
                txtInterestRate.Text = (irate.InterestRate).ToString();

            }
            txtLoanAmount.Text = Appsettings.LoanAmount.ToString();
            txtInterestRate.Text = Appsettings.IntrestRate.ToString();
            txtTermOfLoan.Text = Appsettings.TermOfLoan.ToString();
            txtPrice.Text = Appsettings.Price.ToString();
            txtHomeCoast.Text = Appsettings.HomeCost.ToString();

        }
      
        void txtHomeCoast_MouseLeave(object sender, MouseEventArgs e)
        {
            txtHomeCoast.Foreground = new SolidColorBrush(Colors.Black);
        }

        void txtHomeCoast_MouseEnter(object sender, MouseEventArgs e)
        {
            txtHomeCoast.Foreground = new SolidColorBrush(Colors.White);
        }

        void txtPrice_MouseLeave(object sender, MouseEventArgs e)
        {
            txtPrice.Foreground = new SolidColorBrush(Colors.Black);
        }

        void txtPrice_MouseEnter(object sender, MouseEventArgs e)
        {
            txtPrice.Foreground = new SolidColorBrush(Colors.White);
        }

        void txtTermOfLoan_MouseLeave(object sender, MouseEventArgs e)
        {
            txtTermOfLoan.Foreground = new SolidColorBrush(Colors.Black);
        }

        void txtTermOfLoan_MouseEnter(object sender, MouseEventArgs e)
        {
            txtTermOfLoan.Foreground = new SolidColorBrush(Colors.White);
        }

        void txtInterestRate_MouseLeave(object sender, MouseEventArgs e)
        {
            txtInterestRate.Foreground = new SolidColorBrush(Colors.Black);
        }

        void txtInterestRate_MouseEnter(object sender, MouseEventArgs e)
        {
            txtInterestRate.Foreground = new SolidColorBrush(Colors.White);
        }

        void txtLoanAmount_MouseEnter(object sender, MouseEventArgs e)
        {
            txtLoanAmount.Foreground = new SolidColorBrush(Colors.White);
      
        }

        void txtLoanAmount_MouseLeave(object sender, MouseEventArgs e)
        {

            txtLoanAmount.Foreground = new SolidColorBrush(Colors.Black);
        }

        private void imgTitle_MouseEnter(object sender, MouseEventArgs e)
        {
            NavigationService.Navigate(new Uri("/Views/Result/ResultPage.xaml?sectionId=4&", UriKind.Relative));
        }

        private void REg_ValueChanged_1(object sender, DateTimeValueChangedEventArgs e)
        {
            try
            {
                DateTime date = (DateTime)e.NewDateTime;
                this.REg.Value = date;
                App.Todate = REg.ValueString;
            }
            catch (Exception ex)
            {
            }
        }

        
        //public List<SettingsProperties> LoadItems()
        //{
        //    List<SettingsProperties> p = new List<SettingsProperties>();
        //    try
        //    {
        //        XDocument xdoc = XDocument.Load("DispalyData/settings.xml");

        //        var list = from i in xdoc.Descendants("value") select i;
        //        foreach (var element in list)
        //        {
        //            SettingsProperties objItem = new SettingsProperties();
        //            //objItem.Id = element.Attribute("Id").Value;

        //            //if (objItem.Id == "1")
        //            //{
        //            if (IsolatedSettings.getInterestValue("InterestRate") != "")
        //                objItem.InterestRate = Convert.ToDouble(IsolatedSettings.getInterestValue("InterestRate"));


        //            else
        //                objItem.InterestRate = Convert.ToDouble(element.Attribute("InterestRate").Value);
        //            p.Add(objItem);
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        Exception exNew = new Exception("Exception in LoadItems Method In AboutUs file\n\n" + ex.Message + " \n\n Stack Trace \n" + ex.StackTrace);
        //        throw exNew;
        //    }

        //    return p;
        //}

    }

}